package com.example.mymvvmdemo.models.add_article.adapter;

import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

import com.example.mymvvmdemo.R;
import com.example.mymvvmdemo.base.BaseVBAdapter;
import com.example.mymvvmdemo.databinding.ItemAddarticleBinding;
import com.example.mymvvmdemo.models.add_article.entity.AddArticleEntity;
import com.example.mymvvmdemo.utils.GlideUtils;
import com.example.mymvvmdemo.utils.LogUtils;

import java.util.ArrayList;

public class AddArticleAdapter extends BaseVBAdapter<AddArticleEntity, ItemAddarticleBinding> {

    public AddArticleListener listener;

    public void setListener(AddArticleListener listener) {
        this.listener = listener;
    }

    /**
     * @param context
     */
    public AddArticleAdapter(Context context, ArrayList<AddArticleEntity> list) {
        super(context);
        setDataList(list);
    }

    @Override
    protected void updateData(ItemAddarticleBinding bind, AddArticleEntity item, int position) {

        changeViewWidthHeight(bind);

        boolean havePic = false;
        if (item != null) {
            havePic = item.isHavePic();
        }

        bind.clEmpty.setVisibility(havePic ? View.GONE : View.VISIBLE);
        bind.clImage.setVisibility(havePic ? View.VISIBLE : View.GONE);
        if (havePic) {
            havePicView(bind, item, position);
        } else {
            emptyView(bind, item, position);
        }

    }

    private void changeViewWidthHeight(ItemAddarticleBinding bind) {


        // 使用ViewTreeObserver来监听布局完成事件
        bind.clEmpty.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // 确保只执行一次
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    bind.clEmpty.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    bind.clEmpty.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }

                int width = bind.clEmpty.getWidth();
                ViewGroup.LayoutParams params = bind.clEmpty.getLayoutParams();
                params.height = width; // 设置高度等于宽度
                bind.clEmpty.setLayoutParams(params);
            }
        });

        bind.clImage.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // 确保只执行一次
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    bind.clImage.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    bind.clImage.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }

                int width = bind.clImage.getWidth();
                ViewGroup.LayoutParams params = bind.clImage.getLayoutParams();
                params.height = width; // 设置高度等于宽度
                bind.clImage.setLayoutParams(params);
            }
        });

    }

    private void emptyView(ItemAddarticleBinding bind, AddArticleEntity item, int position) {
        bind.clEmpty.setOnClickListener(view -> {
            if (listener != null) {
                listener.onAddArticleClick(position);
            }
        });
    }

    private void havePicView(ItemAddarticleBinding bind, AddArticleEntity item, int position) {
        Uri imageUri = item.getImageUri();
        if (imageUri != null) {
            // Load image from URI
            GlideUtils.loadImage(context, imageUri, R.drawable.ic_svg_add_black, bind.ivImage);
        } else {
            // Fallback to resource ID if URI is not available
            int resId = item.getResId();
            GlideUtils.loadImage(context, resId, R.drawable.ic_svg_add_black, bind.ivImage);
        }
        
        // Add long click listener for image deletion or other operations if needed
        bind.clImage.setOnLongClickListener(v -> {
            // Handle long click if needed (e.g., show delete option)
            return true;
        });
    }

    public interface AddArticleListener {
        void onAddArticleClick(int position);
    }

}
